﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data;
using Pub;

namespace JCNT.NoteWeb.WebII
{
    public partial class Default : System.Web.UI.Page
    {
        StringBuilder json = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            CDebug.Send(Request.Url.ToString());
            if (Request["parentid"] != null)
            {
                WriteTree(Request["parentid"].ToString());
            }
        }

        public void WriteTree(string ParentID)
        {
            CDebug.EnterMethod("生成tree json数据");
            try
            {
                json.Append("{\"success\":true,\"nodes\":[");
                ListSubNode(ParentID);
                json.Remove(json.Length - 3, 3);
                json.Append("]}");
                Response.Write(json);
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }
            CDebug.ExitMethod("生成tree json数据");
            Response.End();
        }
        public void ListSubNode(string RootID)
        {
            CADO ado1 = new CADO();
            string strSQL = "Select * From WP_NLab Where vis=1 and RootID='" + RootID + "' Order By Lab";
            DataSet ds1 = ado1.sel1(strSQL);
            CDebug.Send(strSQL + "\n--返回结果个数: " + ado1.ds_count.ToString());
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                DataRow dr1 = ds1.Tables[0].Rows[i];
                string Lab = dr1["Lab"].ToString();
                string ID = dr1["ID"].ToString();
                string sIcon = "";
                string sUrl = "";
                Lab = Lab.Replace("\r", "");
                Lab = Lab.Replace("\n", "");
                if (isLeafNode(ID))
                {
                    json.Append("{\"id\":" + ID + ",\"text\":\"" + Lab + "\",\"iconCls\":\"" + sIcon + "\", \"state\": \"closed\",\"attributes\":{\"url\":\"" + sUrl + "\"}},");
                    //json.Append("\"children\":[");
                    //ListSubNode(ID);
                }
                else
                {
                    json.Append("{\"id\":" + ID + ",\"text\":\"" + Lab + "\",\"iconCls\":\"" + sIcon + "\", \"state\": \"closed\",\"attributes\":{\"url\":\"" + sUrl + "\"}},");
                }
            }

            string strSQL2 = "Select ID, Tit From WP_Note Where RootID = " + RootID + " Order By Lab";
            DataSet ds2 = ado1.sel1(strSQL2);
            CDebug.Send(strSQL2 + "\n--返回结果个数: " + ado1.ds_count.ToString());
            foreach (DataRow dr2 in ds2.Tables[0].Rows)
            {
                TreeNode node2 = new TreeNode();
                string Tit = dr2["Tit"].ToString();
                string tID = dr2["ID"].ToString();
                string sIcon = "";
                string sUrl = "../Web/Right.aspx?id=" + tID;
                Tit = Tit.Replace("\r", "");
                Tit = Tit.Replace("\n", "");
                json.Append("{\"id\":" + tID + ",\"text\":\"" + Tit + "\",\"iconCls\":\"" + sIcon + "\",\"attributes\":{\"url\":\"" + sUrl + "\"}},");
            }

            json.Remove(json.Length - 1, 1);
            json.Append("]},");
        }

        public bool isLeafNode(string RootID)
        {
            bool isLeaf = false;
            CADO ado1 = new CADO();
            string strSQL = "Select * From WP_NLab Where vis=1 and RootID='" + RootID + "' Order By Lab";
            DataSet ds1 = ado1.sel1(strSQL);
            if (!CDB.dsIsNull(ds1) && ds1.Tables[0].Rows.Count > 0) isLeaf = true;
            return isLeaf;
        }
    }
}
